define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'bootstrap-table-jumpto'], function ($, undefined, Backend, Table, Form, Jumpto) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'salesman/register/index' + location.search,
                    add_url: 'salesman/register/add',
                    edit_url: 'salesman/register/edit',
                    del_url: 'salesman/register/del',
                    multi_url: 'salesman/register/multi',
                    table: 'salesman_register',
                }
            });

            var table = $("#table");

            $('.my-create-file').click(function () {
                var options = table.bootstrapTable('getOptions');
                var search = options.queryParams({});

                var filter = search.filter;
                var op = search.op;
                var url =  'salesman/register/exportXlsx?filter=' + filter + '&op=' + op;
                url = Fast.api.fixurl(url);
                window.open(url);
            });
            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                showExport: false,
                showJumpto: true,
                searchFormVisible: true,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'user_id', title: '业务员ID'},
                        {field: 'user.nickname', title: '业务员昵称', operate: 'like'},
                        {field: 'user.mobile', title: '业务员电话', operate: 'like'},
                        {field: 'name', title: __('Name'), operate: 'like'},
                        {field: 'mobile', title: __('Mobile'), operate: 'like'},
                        {field: 'company_name', title: __('Company_name'), operate: 'like', cellStyle:Controller.api.css.str},
                        {field: 'address', title: '地址', operate: false},
                        {field: 'type_data.str', title: '品种&数量', operate:false, cellStyle:Controller.api.css.address},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'matching',
                                    text: '匹配订单',
                                    classname: 'btn btn-xs btn-warning btn-dialog',
                                    icon: 'fa fa-gg-circle',
                                    extend: 'data-area=\'["100%", "100%"]\'',
                                    url: 'salesman/statistics/index?register_id={ids}'
                                },
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'salesman/register/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'name', title: __('Name'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'salesman/register/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'salesman/register/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            },
            css: {
                str: function () {
                    return {
                        css: {
                            'max-width': '100px',
                            'max-height': '150px'
                        }
                    };
                },
                address: function () {
                    return {
                        css: {
                            'max-width': '100px',
                            'max-height': '150px'
                        }
                    };
                }
            }
        }
    };
    return Controller;
});